import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);
// 重定向发生报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch(err => err);
};
const routes = [
  {
    path: "/",
    redirect: "/home"
  },
  {
    path: "/home",
    name: "home",
    component: () => import("../views/home/index.vue")
  },
  {
    path: "/category",
    name: "category",
    component: () => import("../views/category/index.vue")
  },
  {
    path: "/addressList",
    name: "addressList",
    component: () => import("../views/address/index.vue")
  },
  {
    path: "/addressEdit",
    name: "addressEdit",
    component: () => import("../views/addressEdit/index.vue")
  },
  {
    path: "/cart",
    name: "cart",
    component: () => import("../views/cart/index.vue")
  },
  {
    path: "/surprise",
    name: "surprise",
    component: () => import("../views/surprise/index.vue")
  },
  {
    path: "/choosepay",
    name: "choosepay",
    component: () => import("../views/choosepay/index.vue"),
    children: [{
      path: "pay",
      name: "pay",
      component: () => import("../views/pay/index.vue")
    }]
  },
  {
    path: "/search",
    name: "search",
    component: () => import("../components/search.vue")
  },
  {
    path: "/searchList",
    name: "searchList",
    component: () => import("../components/searchList.vue")
  },
  {
    path: "/info",
    name: "info",
    component: () => import("../views/info/index.vue")
  },
  {
    path: "/order",
    name: "order",
    component: () => import("../views/order/index.vue"),
    meta: {
      keepAlive: false
    }
  },
  {
    path: "/my",
    name: "my",
    component: () => import("../views/my/index.vue")
  },
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
  // 路由跳转 默认回到顶部
  scrollBehavior (to, from, savedPosition) {
    console.log(to, from, savedPosition)
    return { x: 0, y: 0 }
  }
});

export default router;
